<?php

namespace app\admin\model;

use think\Model;


class UserConfig extends Model
{

    

    

    // 表名
    protected $name = 'user_config';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        'delete_time_text'
    ];
    public function getList($type = null)
    {
        $config = [
            'general' => [
                [
                    'name' => '押金类型',
                    'field' => 'deposit_type',
                ],
                [
                    'name' => '固定押金',
                    'field' => 'deposit_money',
                ],
                [
                    'name' => '需要抄表',
                    'field' => 'need_cb',
                ],
                [
                    'name' => '统一交租日',
                    'field' => 'rent_day_switch',
                ],
                [
                    'name' => '交租日',
                    'field' => 'rent_day',
                ],
                [
                    'name' => '提前X天交租',
                    'field' => 'ahead_day',
                ],
                [
                    'name' => '短信通知',
                    'field' => 'sms_notice',
                ],
                [
                    'name' => '关注欢迎词',
                    'field' => 'welcome',
                ],
            ],
            'contract' => [
                [
                    'name' => '租期1个月及以内，到期前',
                    'field' => 'one_month',
                ],
                [
                    'name' => '租期6个月及以内，到期前',
                    'field' => 'six_month',
                ],
                [
                    'name' => '租期6个月以上，到期前',
                    'field' => 'gt_six_month',
                ],
                [
                    'name' => '自动续租',
                    'field' => 'auto_pay',
                ],
                [
                    'name' => '到期前',
                    'field' => 'ahead_auto_pay',
                ],
            ],
            'bill' => [
                [
                    'name' => '包含押金',
                    'field' => 'include_deposit',
                ],
                [
                    'name' => '自动生成',
                    'field' => 'auto_generate',
                ],
                [
                    'name' => '金额保留',
                    'field' => 'amount_decimal',
                ],
                [
                    'name' => '余额抵扣',
                    'field' => 'balance_deduction',
                ],
                [
                    'name' => '默认备注',
                    'field' => 'remark',
                ],
            ],

        ];

        return $type ? ($config[$type] ?? []) : $config;
    }

    public function getType($field)
    {
        // 遍历所有配置分组（general、contract、bill等）
        foreach ($this->getList() as $group => $fields) {
            // 遍历当前分组下的所有字段配置
            foreach ($fields as $fieldConfig) {
                // 找到匹配的字段
                if ($fieldConfig['field'] === $field) {
                    return $group; // 返回对应的分组名
                }
            }
        }
        return null; // 如果未找到，返回null
    }

    public function getUserConfig($user_id, $type, $field = null)
    {
        if (is_null($field)) {
            return $this->where([
                'user_id' => $user_id,
                'type' => $type,
            ])->column('value', 'field');
        } else {
            return $this->where([
                'user_id' => $user_id,
                'type' => $type,
                'field' => $field,
            ])->value('value');
        }
    }

    function findNameByField($config, $targetField) {
        // 遍历所有配置分组
        foreach ($config as $group => $fields) {
            // 遍历当前分组下的所有字段配置
            foreach ($fields as $fieldConfig) {
                // 找到匹配的字段
                if ($fieldConfig['field'] === $targetField) {
                    return $fieldConfig['name']; // 返回对应的name值
                }
            }
        }
        return null; // 未找到时返回null
    }


    public function saveUserConfig($user_id, $data)
    {
        foreach ($data as $k => $v) {
            if (empty($v) && $v != 0) {
                continue;
            }
            $row = self::get(['user_id' => $user_id, 'field' => $k]);
            if (!$row) {
                $config = $this->getList();
                $type = $this->getType($k);
                $info = [
                    'user_id' => $user_id,
                    'type' => $type,
                    'name' => $this->findNameByField($config, $k),
                    'field' => $k,
                    'value' => $v,
                ];
                self::create($info);
            } else {
                $row->value = $v;
                $row->save();
            }

        }
    }



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delete_time']) ? $data['delete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
